package pingan.com.imandtwo.fragments.newsfragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.bean.Shouye;
import pingan.com.imandtwo.beans.BannerBean;
import pingan.com.imandtwo.fragments.newsfragments.one.RecyclerAdpater;
import pingan.com.imandtwo.fragments.newsfragments.one.Recycler_Chuanxing;
import pingan.com.imandtwo.g_mvp.presenter.Presenter;
import pingan.com.imandtwo.g_mvp.view.IView;
import pingan.com.imandtwo.interfaces.Iview;
import pingan.com.imandtwo.presenter.HttpPresenter;
import pingan.com.imandtwo.utils.BannerImageLoader;

import static android.support.v7.widget.LinearLayoutCompat.VERTICAL;


/**
 * Created by xsj on 2018/2/1.
 */

public class OneFragment extends Fragment implements Iview, IView {
    @BindView(R.id.banner)
    Banner banner;
    Unbinder unbinder;
    @BindView(R.id.del)
    TextView del;
    @BindView(R.id.recy)
    RecyclerView recy;
    @BindView(R.id.del_two)
    TextView delTwo;
    @BindView(R.id.recy_two)
    RecyclerView recyTwo;
    private HttpPresenter httpPresenter;
    private Presenter presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item1, container, false);
        unbinder = ButterKnife.bind(this, view);

        //网络请求轮播
        httpPresenter = new HttpPresenter(this);
        HashMap<String, String> map = new HashMap<>();
        httpPresenter.getMap(map, "轮播");
        //请求网络
        initview();
        return view;

    }

    private void initview() {
        presenter = new Presenter();
        presenter.attachView(this);
        HashMap<String, String> map = new HashMap<>();
        // http://api.tianapi.com/vr/?key=4a82473783694ddebd495d47b16c906e&num=10
        map.put("key", "4a82473783694ddebd495d47b16c906e");
        map.put("num", "2");
        presenter.getData("http://api.tianapi.com/", map);
    }

    @Override
    public void onSuccess(Object o, String tag) {
        if (tag.equals("轮播")) {

            BannerBean bannerBean = (BannerBean) o;
            String status = bannerBean.getStatus();
            if (status.equals("0000")) {
                List<String> list = new ArrayList<>();
                List<BannerBean.ResultBean> result = bannerBean.getResult();
                for (int i = 0; i < result.size(); i++) {
                    list.add(result.get(i).getImage());
                }

                banner.setImageLoader(new BannerImageLoader()); //自己建一个类
                banner.setImages(list); //自己建一个list集合放图片
                banner.setDelayTime(2000);//几秒切换一次图片
                banner.start();
            }


        }
    }


    @Override
    public void success(Object o) {
        if (o instanceof List) {
            List<Shouye.NewslistBean> newslist = (List<Shouye.NewslistBean>) o;
            if (newslist != null) {
                RecyclerAdpater recyclerAdpater = new RecyclerAdpater(getActivity(), newslist);

                recy.setAdapter(recyclerAdpater);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), VERTICAL, false);
                recy.setLayoutManager(linearLayoutManager);
                recyclerAdpater.notifyDataSetChanged();

                Recycler_Chuanxing Recycler_Chuanxing = new Recycler_Chuanxing(getActivity(), newslist);
                recyTwo.setAdapter(Recycler_Chuanxing);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2 , VERTICAL,false);
                recyTwo.setLayoutManager(gridLayoutManager);
                Recycler_Chuanxing.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void Failes(Exception e) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        httpPresenter.disAttach();
    }
}
